<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/30
 * Time: 15:56
 */
/**
 * 店铺设置
 */
namespace Member\Controller;
class ShopController extends CommonController {
    public function _initialize(){
        parent::_initialize();
        $this->is_login();
    }
    public function index (){
        $model = M ('shop');
        $arr = array ();
        $table = array ('yk_shop'=>'s','yk_area_cate'=>'a');
        $map = array (
            's.status=1',
            's.area_id=a.id'
        );
        $count = $model->field ('s.id')->table ($table)->where ($map)->count ();
        $pager = new \Org\Util\Page($count, 15);
        $arr['count'] = $count;
        $arr['page'] = $pager->show();
        $arr['list'] = $model->field ('s.*,a.name')
            ->table ($table)
            ->where ($map)
            ->order ('s.add_time desc')
            ->limit ($pager->firstRow.','.$pager->listRows)
            ->select ();

        $this->assign ('list', $arr);
        $this->display ();
    }
    public function edit (){
        if (IS_POST){
            $data = I ('post.data', '');;
            $res = M ('shop')->save($data);
            if ($res){
                $this->success('编辑成功', U('Shop/index'));
            }else{
                $this->error('编辑失败');
            }
        }
        $id = I ('get.id', '');
        $model = M ('shop');
        $table = array ('yk_shop'=>'s','yk_area_cate'=>'a');
        $map = array (
            's.status=1',
            's.id' =>$id,
            's.area_id=a.id'
        );
        $data = $model->field ('s.*,a.name')->table ($table)->where ($map)->find ();
        $this->assign ('info', $data);
        $this->display ();
    }
    public function del (){
        $mod = M ('Shop');
        $ids = I ('get.id', '');
        if (false !== $mod->delete($ids)){
            $this->success('删除成功', U('Shop/index'));
        }else{
            $this->error('删除失败');
        }
    }
    public function group (){
        $model = M ('division');
        $arr = array ();
        $map = array (
            'status=1'
        );
        $count = $model->field ('id')->where ($map)->count ();
        $pager = new \Org\Util\Page($count, 15);
        $arr['count'] = $count;
        $arr['page'] = $pager->show();
        $arr['list'] = $model
            ->where ($map)
            ->order ('add_time asc')
            ->limit ($pager->firstRow.','.$pager->listRows)
            ->select ();

        $this->assign ('list', $arr);
        $this->display ();
    }
    public function edit_group (){
        if (IS_POST){
            $data = I ('post.data', '');;
            $res = M ('division')->save($data);
            if ($res){
                $this->success('编辑成功', U('Shop/group'));
            }else{
                $this->error('编辑失败');
            }
        }
        $id = I ('get.id', '');
        $model = M ('division');
        $map = array (
            'status=1',
            'id' =>$id,
        );
        $data = $model->where ($map)->find ();
        $this->assign ('info', $data);
        $this->display ();
    }
    public function del_group (){
        $mod = M ('division');
        $ids = I ('get.id', '');
        if (false !== $mod->delete($ids)){
            $this->success('删除成功', U('Shop/group'));
        }else{
            $this->error('删除失败');
        }
    }
    public function add (){
        if (IS_POST){
            $data = I ('post.data', '');;
            $data['add_time'] = date ('Y-m-d H:i:s');
            $res = M ('shop')->add($data);
            if ($res){
                $this->success('编辑成功', U('Shop/index'));
            }else{
                $this->error('编辑失败');
            }
        }

        $this->display ();
    }
    public function increased (){
        if (IS_POST){
            $data = I ('post.data', '');
            $data['add_time'] = time();
            $res = M ('division')->add($data);
            if ($res){
                $this->success('添加成功', U('Shop/group'));
            }else{
                $this->error('添加失败');
            }
        }

        $this->display ();
    }

}
